package com.wx.mall.sys.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wx.mall.sys.entity.Permission;
import com.wx.mall.sys.mapper.PermissionMapper;
import com.wx.mall.sys.service.IPermissionService;
import com.wx.mall.sys.vo.resp.PermissionRespNodeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author j
 * @since 2020-03-18
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public List<PermissionRespNodeVO> findAllByTree() {
        List<Permission> permissions = baseMapper.selectList(null);
        List<PermissionRespNodeVO> tree = getTree(permissions);
        return tree;
    }

    /**
     * 递归获取菜单树
     *
     * @param all
     * @return
     */
    private List<PermissionRespNodeVO> getTree(List<Permission> all) {
        //类型转换
        List<PermissionRespNodeVO> permissionRespNodeVOS = JSON.parseArray(JSON.toJSONString(all), PermissionRespNodeVO.class);
        List<PermissionRespNodeVO> list = new ArrayList<>();
        if (all.isEmpty()) {
            return list;
        }
        for (PermissionRespNodeVO respNodeVO : permissionRespNodeVOS) {
            if (respNodeVO.getParentId() == 0) {
                respNodeVO.setChildren(getChild(respNodeVO.getId(), all));
                list.add(respNodeVO);
            }
        }
//        for (Permission sysPermission : all) {
//            if (sysPermission.getParentId() == 0) {
//                PermissionRespNodeVO respNodeVO = new PermissionRespNodeVO();
//                BeanUtils.copyProperties(sysPermission, respNodeVO);
//                respNodeVO.setTitle(sysPermission.getName());
//                respNodeVO.setChildren(getChild(sysPermission.getId(), all));
//                list.add(respNodeVO);
//            }
//        }

        return list;
    }

    /**
     * 递归遍历所有数据
     *
     * @param id
     * @return
     */
    private List<PermissionRespNodeVO> getChild(Long id, List<Permission> all) {
        List<PermissionRespNodeVO> permissionRespNodeVOS = JSON.parseArray(JSON.toJSONString(all), PermissionRespNodeVO.class);
        List<PermissionRespNodeVO> list = new ArrayList<>();
        for (PermissionRespNodeVO respNodeVO : permissionRespNodeVOS) {
            if (respNodeVO.getParentId() == id) {
                respNodeVO.setChildren(getChild(respNodeVO.getId(), all));
                list.add(respNodeVO);
            }
        }
        return list;
    }
//    private List<PermissionRespNodeVO> getChild(Long id, List<Permission> all) {
//        List<PermissionRespNodeVO> list = new ArrayList<>();
//        for (Permission sysPermission : all) {
//            if (sysPermission.getParentId() == id) {
//                PermissionRespNodeVO respNodeVO = new PermissionRespNodeVO();
//                BeanUtils.copyProperties(sysPermission, respNodeVO);
//                respNodeVO.setTitle(sysPermission.getName());
//                respNodeVO.setChildren(getChild(sysPermission.getId(), all));
//                list.add(respNodeVO);
//            }
//        }
//        return list;
//    }
}
